﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CompanyGoodsList_sc : BasePage
    {
        private string strcompanynamesc = "";//企业名称
        private string strMosth = "";//月份
        private string strsortname = "";//类别
        string SortField = "ProductName ASC";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();
            }

        }
        void BindList()
        {
            SuperviseQuery query = ViewState["query"] as SuperviseQuery;

            strcompanynamesc = query.CompanyName_SC;
            strMosth = query.Mid;
            strsortname = query.SortName;
            if (!strMosth.Equals("")) //如果月份为空 参数有误
            {
                SuperviseQuery sq = new SuperviseQuery();
                sq.CompanyName_SC = strcompanynamesc;
                sq.SortName = strsortname;
                sq.Append = Function.FormatPlanTime("PlanTime", strMosth);

                sq.ProductName = HZ.Fun.CheckString(this.txtProductName.Text.Trim());
                sq.MedicineModelName = HZ.Fun.CheckString(this.txtMedicinemodel.Text.Trim());
                sq.Pack = HZ.Fun.CheckString(this.txtPack.Text.Trim());
                sq.Unit = HZ.Fun.CheckString(this.txtUnit.Text.Trim());

                int t = 0;
                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
                ie.QueryObject = new HZ.Supervise.Model.QueryObject()
                {
                    //分组字段
                    GroupField = GroupByField.ProcureCatalogID,
                    //查询条件
                    Query = sq,
                    //分组查询字段
                    GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeCountTotal | GroupFields.PurchaseCountTotal | GroupFields.DistributeRate,
                    //排序字段
                    SortField = SortField
                };

                //将数据填入gridview中
                System.Data.DataTable dt = ie.ExecutePage(1, 0, out t);
                gvwGoods.DataSource = dt;
                gvwGoods.DataBind();
                //for (int j = 0; j < gvwGoods.Rows.Count; j++)
                //{
                //    //名称相同才能合并
                //    //防止超出索引
                //    //if (j < (gvwGoods.Rows.Count - 1))
                //    //{
                //    //    if (gvwGoods.Rows[j].Cells[0].Text.Trim() == gvwGoods.Rows[j + 1].Cells[0].Text.Trim())
                //    //    {
                //    //        //合并头
                //    //        gvwGoods.Rows[j].Cells[0].RowSpan = 2;
                //    //        gvwGoods.Rows[j + 1].Cells[0].Visible = false;

                //    //        //合并尾
                //    //        gvwGoods.Rows[j].Cells[8].RowSpan = 2;
                //    //        gvwGoods.Rows[j + 1].Cells[8].Visible = false;
                //    //        j++;
                //    //    }
                //    //    else
                //    //    {

                //    //    }
                //    //}

                //}

            }
            else
            {
                Response.Write("<script>history.go(-1);<script>");
            }
        }

        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }

        protected void gvwGoods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyGoodsDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.ProcureCatalogID = e.CommandArgument.ToString();

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.ProcureCatalogID))
                {
                    Response.Redirect(string.Format("CompanyGoodsDetail_sc.aspx?sessionid={0}", sessionid));
                }
                else
                {

                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SortField = this.droSortField.SelectedValue;
            AspNetPager1.CurrentPageIndex = 1;
            BindList();
        }
    }
}
